package JUC;

import java.util.concurrent.CountDownLatch;

/**
 * @author 豆包LU
 * @date 2023/5/23 11:48
 */
public class OrderedThreadExecution1 {
    private static CountDownLatch latch = new CountDownLatch(1);
    private static CountDownLatch latch1 = new CountDownLatch(1);

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("T1");
                latch.countDown();
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    latch.await(); // 等待latch倒计数到0
                    System.out.println("T2");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    latch1.countDown();
                }
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    latch1.await(); // 等待latch倒计数到0
                    System.out.println("T3");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 启动线程
        t1.start();
        t2.start();
        t3.start();
    }
}
